package com.budwk.app.sqfwV2.generator.entrepreneurShipHouse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.dto.EntrepreneurShipHouseParamDTO;
import com.budwk.app.util.poiUtils.SitePictureOfficeUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 图片预览 生成pdf 2.0后操作
 */

@Slf4j
public class EntrepreneurShipPictureGenerator extends BasePdfGenerator {

    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return isGenerate(pdfGenerateParam);
    }

    // 檢查是否有隐患图片
    private boolean isGenerate(PdfGenerateParam pdfGenerateParam) {
        EntrepreneurShipHouseParamDTO field = (EntrepreneurShipHouseParamDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        String inspectionPhotos = field.getInspectionPhotos();
        if (StringUtils.isNotEmpty(inspectionPhotos)) {
            return true;
        }
        return false;
    }

    private List<SitePictureOfficeUtils.SitePictureContent> ansImage(List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList) {
        // 最多取9张图片
        if (sitePictureContentList.size() < 9) {
            return sitePictureContentList;
        }

        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContents = sitePictureContentList.subList(0, 9);
        return sitePictureContents;
    }


    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList = Lists.newArrayList();
        EntrepreneurShipHouseParamDTO pdfDTO = (EntrepreneurShipHouseParamDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        String visitPictures = pdfDTO.getInspectionPhotos();
        if (StringUtils.isNotBlank(visitPictures)) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(visitPictures.split(",")));
            String textStr = "附件：{} {} 入户照片";
            textStr = StrFormatter.format(textStr, pdfDTO.getPlaceName(), DateUtil.format(pdfDTO.getFillDate(), "yyyy-MM-dd"));
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
        }
        // 最多取9张图片
        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContents = ansImage(sitePictureContentList);
        Map<String, Object> entityMap = SitePictureOfficeUtils.popularParamMap(sitePictureContents);
        if (entityMap != null) {
            paramMap.putAll(entityMap);
        }
    }

    @Override
    protected String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return CommonConstants.PDF_NULL_TEMPLATE_PATH;
    }
}
